package cn.lhx.blog.action;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.UploadFormBean;
import cn.lhx.blog.pojo.UploadItem;
import cn.lhx.blog.service.UploadItemService;

public class UploadManager extends DispatchAction{
@Resource
private UploadItemService uploadService;
	
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("初始化加载");
		
		List<UploadItem> uploadItems=new ArrayList<UploadItem>();
		uploadItems=uploadService.getUploadItems(0, 5);
		request.setAttribute("uploadItems", uploadItems);
		
		return mapping.findForward("upload");
	}
	
	public ActionForward saveUploadInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("保存上传信息");
		UploadFormBean ulf=(UploadFormBean)form;
		HashMap<String, String> formFileds=(HashMap<String,String>)request.getAttribute("formFileds");
		List<File> upokFiles=(ArrayList<File>)request.getAttribute("upokFiles");
		String fileName=new String(formFileds.get("name_1").getBytes("ISO-8859-1"),"utf-8");
		String fileDescription=new String(formFileds.get("description_1").getBytes("ISO-8859-1"),"utf-8");
		File file=upokFiles.get(0);
		
		UploadItem uploadItem=new UploadItem();
		uploadItem.setName(fileName);
		uploadItem.setDescription(fileDescription);
		uploadItem.setPhysicalPath(file.getAbsolutePath());
		//设置服务器访问路径
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		String servicerPath=file.getAbsolutePath().substring(file.getAbsolutePath().indexOf("attachment"));
		servicerPath=basePath+servicerPath;
//		System.out.println(servicerPath);
		servicerPath=servicerPath.replace("\\", "/");
//		System.out.println(servicerPath+"ddd");
		uploadItem.setServicerPath(servicerPath);
		uploadItem.setUploadTime(new Date());
		
//		System.out.println("fileName:"+fileName+"\n description: "+fileDescription);
		uploadService.addUploadItem(uploadItem);
		
		return mapping.findForward("uploadOK");
	}
	
	public ActionForward initManager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("初始化上传管理");
		List<UploadItem> uploadItems=new ArrayList<UploadItem>();
		uploadItems=uploadService.getUploadItems(0, 10);
		request.setAttribute("uploadItems", uploadItems);
		
		
		return mapping.findForward("upload_manager");
		
	}
	/**
	 * 删除上传文件
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("删除上传文件");
		UploadFormBean ulf=(UploadFormBean)form;
		UploadItem item=uploadService.getUptItem(ulf.getId());
		//删除文件实体
		File file=new File(item.getPhysicalPath());
		if(file.exists()){
			file.delete();
		}
		
		//删除上传文件
		uploadService.delUploadItem(ulf.getId());
		
		return mapping.findForward("deleteOK");
	}
	public static void main(String[] args) {
//		String s="dsfds\\";
//		System.out.println("s= "+s);
//		s.replace("\\", "//");
		
//		System.out.println("s2= "+s.replace("\\", "/"));
	}

}
